"""
程序入口模块
"""


from interface import User, Query, Result


ACCOUNT_FILE = './accounts.txt'
RESULTS_FILE = './results.txt'


def run():
    # 生成账号对象字典
    accounts = {account['username']: User(*account.values()) for account in Query.load_file(ACCOUNT_FILE)}

    # 生成成绩对象字典
    results = {result['name']: Result(*result.values()) for result in Query.load_file(RESULTS_FILE)}

    # 创建查询对象
    query = Query()

    if query.login(accounts):
        if query.user.role == 2:
            choice = input("老师您好！是否需要登记考试成绩？q退出，任意键继续")
            if choice.lower() != 'q':

                while True:
                    name = input("请输入学生姓名：")
                    if name in results:
                        print("该学生成绩已经记录！")
                        continue
                    else:
                        try:
                            results[name] = query.record_scores(name)
                        except ValueError:
                            print("请重新登记！")
                            continue
                        query.write_file(RESULTS_FILE, results[name].__dict__)
                        choice = input("继续登记考试成绩？q退出，任意键继续")
                        if choice.lower() == 'q':
                            break
                        else:
                            continue

        while True:
            query_name = input("请输入要查询成绩的学生姓名：")
            if query_name not in results:
                print("查询的学生不存在，请重新输入")
                continue
            else:
                print(results[query_name].__dict__)
                choice = input("继续登记考试成绩？q退出，任意键继续")
                if choice.lower() == 'q':
                    break
        print("程序退出！")


if __name__ == '__main__':
    run()
